﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.IO;
using System.Xml;
using System.Net;
using System.Web;


public class MyFunction
{

    public static string MahoaMatKhau(string pass)
    {
        UnicodeEncoding encoding = new UnicodeEncoding();
        Byte[] hashBytes = encoding.GetBytes(pass);
        SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider();
        Byte[] cryptPassword = sha1.ComputeHash(hashBytes);
        return BitConverter.ToString(cryptPassword);
    }
    public static string stringRandom(int num)
    {
        int length = num;
        string chars = "Q4WE2RT9YUI1P7ASF8GH5JKL3ZXC6VBNM123456789";
        Random ran = new Random();
        string mystr = "";
        for (int i = 0; i < length; i++)
        {
            char str = chars[ran.Next(chars.Length)];
            mystr += str.ToString();
        }
        return mystr;
    }
    public static string moneyToString(int money)
    {
        char[] strMoney = money.ToString().ToCharArray();
        string result = "";
        int i = strMoney.Length;
        int k = 3;
        for (i = i - 1; i > -1; i--)
        {
            k--;
            result = strMoney[i] + result;
            if (k == 0)
            {
                k = 3;
                if (i > 1)
                    result = '.' + result;
            }

        }

        return result;
    }

    internal static void CopyFile(string sourcePatch, string targetPath, string filename, string newFileName)
    {
        string sourceFile = System.IO.Path.Combine(sourcePatch, filename);
        string destFile = System.IO.Path.Combine(targetPath, newFileName + filename.Remove(0, filename.LastIndexOf(".")));
        if (!System.IO.Directory.Exists(targetPath))
        {
            System.IO.Directory.CreateDirectory(targetPath);
        }
        try
        {
            System.IO.File.Copy(sourceFile, destFile, true);
        }
        catch (Exception)
        {

        }
    }
    internal static bool CheckFileExist(string path)
    {
        if (File.Exists(path))
            return true;
        else
            return false;
    }
    public static string getXSLT(string patchXML, string patchXSLT)
    {
        string innerHTML = "";
        System.IO.StringWriter str = new System.IO.StringWriter();
        string xmlPath = patchXML;
        string xslPath = patchXSLT;
        System.Xml.XPath.XPathDocument doc = new System.Xml.XPath.XPathDocument(xmlPath);
        System.Xml.Xsl.XslTransform transform = new System.Xml.Xsl.XslTransform();
        transform.Load(xslPath);
        XmlTextWriter xmlWwriter = new XmlTextWriter(str);
        xmlWwriter.Formatting = Formatting.Indented;
        xmlWwriter.Indentation = 4;
        transform.Transform(doc, null, xmlWwriter);
        xmlWwriter.Flush();
        xmlWwriter.Close();
        innerHTML = str.ToString();
        str.Close();
        return innerHTML;
    }
    public static void Show(string message)
    {
        // Cleans the message to allow single quotation marks 
        string cleanMessage = message.Replace("'", "\\'");
        string script = "<script type=\"text/javascript\">alert('" + cleanMessage + "');</script>";

        // Gets the executing web page 
        System.Web.UI.Page page = System.Web.HttpContext.Current.CurrentHandler as System.Web.UI.Page;

        // Checks if the handler is a Page and that the script isn't allready on the Page 
        if (page != null && !page.ClientScript.IsClientScriptBlockRegistered("alert"))
        {
            page.ClientScript.RegisterClientScriptBlock(typeof(MyFunction), "alert", script);
        }
    }
    public static List<System.Web.UI.Control> getAllcontrol(System.Web.UI.Control control, Type type)
    {
        List<System.Web.UI.Control> controls = new List<System.Web.UI.Control>();
        foreach (System.Web.UI.Control ctr in control.Controls)
        {
            if (control.GetType() == type)
            {
                controls.Add(control);
            }
        }
        return controls;
    }
    private static readonly string[] VietNamChar = new string[] 
    { 
        "aAeEoOuUiIdDyY", 
        "áàạảãâấầậẩẫăắằặẳẵ", 
        "ÁÀẠẢÃÂẤẦẬẨẪĂẮẰẶẲẴ", 
        "éèẹẻẽêếềệểễ", 
        "ÉÈẸẺẼÊẾỀỆỂỄ", 
        "óòọỏõôốồộổỗơớờợởỡ", 
        "ÓÒỌỎÕÔỐỒỘỔỖƠỚỜỢỞỠ", 
        "úùụủũưứừựửữ", 
        "ÚÙỤỦŨƯỨỪỰỬỮ", 
        "íìịỉĩ", 
        "ÍÌỊỈĨ", 
        "đ", 
        "Đ", 
        "ýỳỵỷỹ", 
        "ÝỲỴỶỸ" 
    };

    public static string LocDau(string str)
    {
        //Thay thế và lọc dấu từng char      
        for (int i = 1; i < VietNamChar.Length; i++)
        {
            for (int j = 0; j < VietNamChar[i].Length; j++)
                str = str.Replace(VietNamChar[i][j], VietNamChar[0][i - 1]);
        }
        return str.Replace(' ','-');
    }
    public static string GetIP4Address()
    {
        string IP4Address = String.Empty;

        foreach (IPAddress IPA in Dns.GetHostAddresses(HttpContext.Current.Request.UserHostAddress))
        {
            if (IPA.AddressFamily.ToString() == "InterNetwork")
            {
                IP4Address = IPA.ToString();
                break;
            }
        }

        if (IP4Address != String.Empty)
        {
            return IP4Address;
        }

        foreach (IPAddress IPA in Dns.GetHostAddresses(Dns.GetHostName()))
        {
            if (IPA.AddressFamily.ToString() == "InterNetwork")
            {
                IP4Address = IPA.ToString();
                break;
            }
        }

        return IP4Address;
    }
}

